<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名" prop="nickname">
        <el-input v-model="queryParams.nickname" placeholder="请输入用户名" clearable @keyup.enter="handleQuery" />
      </el-form-item>

      <el-form-item label="手机号" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable @keyup.enter="handleQuery" />
      </el-form-item>

      <el-form-item label="打卡状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="打卡状态" clearable style="width: 240px">
          <el-option v-for="dict in sys_check_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="打卡时间" style="width: 308px;">
        <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" height="800" :data="recordList" @selection-change="handleSelectionChange">
    
      <el-table-column label="主键ID" align="center" prop="id" v-if="false" />
      <el-table-column fixed label="用户名" align="center" prop="nickname" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="打卡标题" align="center" prop="title" />
      <el-table-column label="打卡描述" align="center" prop="description" />
      <el-table-column label="提醒打卡时间" align="center" prop="remindTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.remindTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="打卡状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="sys_check_status" :value="scope.row.status" />
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改用药打卡记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="recordRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="user.id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入user.id" />
        </el-form-item>
        <el-form-item label="打卡标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入打卡标题" />
        </el-form-item>
        <el-form-item label="打卡描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="提醒打卡时间" prop="remindTime">
          <el-date-picker clearable v-model="form.remindTime" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择提醒打卡时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="删除标志:0|未删除,1|已删除" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志:0|未删除,1|已删除" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Record">
import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/mobile/takemedicine";

const { proxy } = getCurrentInstance();
const { sys_check_status } = proxy.useDict("sys_check_status");


const recordList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    phone: null,
    nickname: null,
    status: null,
  },
  rules: {
    userId: [
      { required: true, message: "user.id不能为空", trigger: "blur" }
    ],
    title: [
      { required: true, message: "打卡标题不能为空", trigger: "blur" }
    ],
    description: [
      { required: true, message: "打卡描述不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "打卡状态:0|未打卡,1|已打卡不能为空", trigger: "blur" }
    ],
    delFlag: [
      { required: true, message: "删除标志:0|未删除,1|已删除不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询用药打卡记录列表 */
function getList() {
  loading.value = true;
  listRecord(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
    recordList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: null,
    title: null,
    description: null,
    remindTime: null,
    status: 0,
    delFlag: null,
    createTime: null
  };
  proxy.resetForm("recordRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = null;
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加用药打卡记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getRecord(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改用药打卡记录";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["recordRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateRecord(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addRecord(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除数据项？').then(function () {
    return delRecord(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('mobile/takemedicine/export', {
    ...queryParams.value
  }, `record_${new Date().getTime()}.xlsx`)
}

getList();
</script>
